package com.cyw.note.manage.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.cyw.common.annotation.Log;
import com.cyw.common.annotation.RepeatSubmit;
import com.cyw.common.core.controller.BaseController;
import com.cyw.common.core.domain.PageQuery;
import com.cyw.common.core.domain.R;
import com.cyw.common.core.page.TableDataInfo;
import com.cyw.common.core.validate.AddGroup;
import com.cyw.common.core.validate.EditGroup;
import com.cyw.common.enums.BusinessType;
import com.cyw.common.utils.poi.ExcelUtil;
import com.cyw.note.manage.domain.bo.LinksBo;
import com.cyw.note.manage.domain.vo.LinksVo;
import com.cyw.note.manage.service.ILinksService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 友情链接Controller
 *
 * @author Chenyw
 * @date 2022-08-07
 */
@Validated
@Api(value = "友情链接控制器", tags = {"友情链接管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/links")
public class LinksController extends BaseController {

    private final ILinksService iLinksService;

    /**
     * 查询友情链接列表
     */
    @ApiOperation("查询友情链接列表")
    @SaCheckPermission("system:links:list")
    @GetMapping("/list")
    public TableDataInfo<LinksVo> list(LinksBo bo, PageQuery pageQuery) {
        return iLinksService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出友情链接列表
     */
    @ApiOperation("导出友情链接列表")
    @SaCheckPermission("system:links:export")
    @Log(title = "友情链接", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(LinksBo bo, HttpServletResponse response) {
        List<LinksVo> list = iLinksService.queryList(bo);
        ExcelUtil.exportExcel(list, "友情链接", LinksVo.class, response);
    }

    /**
     * 获取友情链接详细信息
     */
    @ApiOperation("获取友情链接详细信息")
    @SaCheckPermission("system:links:query")
    @GetMapping("/{id}")
    public R<LinksVo> getInfo(@ApiParam("主键")
                                     @NotNull(message = "主键不能为空")
                                     @PathVariable("id") Long id) {
        return R.ok(iLinksService.queryById(id));
    }

    /**
     * 新增友情链接
     */
    @ApiOperation("新增友情链接")
    @SaCheckPermission("system:links:add")
    @Log(title = "友情链接", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody LinksBo bo) {
        return toAjax(iLinksService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改友情链接
     */
    @ApiOperation("修改友情链接")
    @SaCheckPermission("system:links:edit")
    @Log(title = "友情链接", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody LinksBo bo) {
        return toAjax(iLinksService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除友情链接
     */
    @ApiOperation("删除友情链接")
    @SaCheckPermission("system:links:remove")
    @Log(title = "友情链接", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@ApiParam("主键串")
                                       @NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return toAjax(iLinksService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
